package com.jydd.singlekotlinmvvm.comm.base

import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import kotlinx.coroutines.*
import timber.log.Timber

/**
 * created by 韦敏敏
 * on 2020/1/20
 */
open class BaseViewModel : ViewModel() {
    fun <T> request(
        onError : (error: Throwable) -> Unit = {},
        execute : suspend CoroutineScope.() -> T
    ) {
        viewModelScope.launch(errorHandler {
            onError.invoke(it)
        }) {
            withContext(Dispatchers.IO){
                execute()
            }

        }
    }
    private fun errorHandler(onError: (error: Throwable) -> Unit) : CoroutineExceptionHandler {
        return CoroutineExceptionHandler { _, throwable ->
            Timber.d(throwable)
            onError.invoke(throwable)
        }
    }
}